﻿using ConsoleDemo.Application;
using Microsoft.Extensions.DependencyInjection;
using PoemGame.ComputerAnswer.Shared;
using PoemGame.ComputerAnswer.Db;
using PoemGame.Domain.GameAggregate;
using PoemGame.Domain.PlayerAggregate;
using PoemGame.Domain.Services;
using PoemService.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using PoemGame.ApplicationService;
using PoemGame.Repository.Simple;
using PoemService.Db;
using Microsoft.Extensions.Configuration;
using PoemGame.Domain.Services.Factory;

namespace ConsoleDemoDb
{
    public class Utility
    {
        public static IServiceProvider GetService()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            var services = new ServiceCollection();
            var poemGameServices = "PoemGame.Domain.Services.Feihualing,PoemGame.Domain.Services.Duishi,PoemGame.Domain.Services.Jielong".Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            AppDomain currentDomain = AppDomain.CurrentDomain;
            foreach (var item in poemGameServices)
            {
                currentDomain.Load(item);
            }

            var scanners = AppDomain.CurrentDomain.GetAssemblies().ToList()
                    .SelectMany(x => x.GetTypes())
                    .Where(t => t.GetInterfaces().Contains(typeof(IDomainService)) && t.IsClass).ToList();

            foreach (Type type in scanners)
            {
                services.AddScoped(type);
            }
            services.AddScoped<IDomainServiceFactory<ICheckAnswerService>, DomainServiceFactory<ICheckAnswerService>>();
            services.AddScoped<IDomainServiceFactory<ICheckGameConditionService>, DomainServiceFactory<ICheckGameConditionService>>();
            services.AddScoped<IGameFactory, GameFactory>();

            var serviceProvider = services
                .AddSingleton<IPoemService, PoemServiceDb>((provider =>
                {
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = configuration.GetConnectionString("PoemServiceConn");
                    return new PoemServiceDb(conn);
                } ))
                .AddScoped<IGameRepository,GameRepository>()
                .AddScoped<IPlayerRepository, PlayerRepository>()
                .AddScoped<IComputerAnswerFactory, ComputerAnswerFactory>((provider =>
                {
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = configuration.GetConnectionString("ComputerAnswerServiceConn");
                    return new ComputerAnswerFactory(conn);
                }))
                .AddScoped<IConsoleDemoAppService, ConsoleDemoAppService>()
                .BuildServiceProvider();
            return serviceProvider;
        }
    }
}
